﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;
//using System.Math;
using Microsoft.Kinect;
using Microsoft.Win32;
using Kinect.Toolbox;
using Kinect.Toolbox.Gestures;
using System.Threading;
using System.Threading.Tasks;
using Kinect.Toolbox.Record;
using Kinectver1._0;
using SharpGL;
using SharpGL.WPF;
using SharpGL.Serialization;
using SharpGL.RenderContextProviders;
using SharpGL.SceneGraph;
using SharpGL.OpenGLAttributes;
using SharpGL.Enumerations;

namespace Kinectver1._0
{
    //the vertex information of an OFF file will be stored here!
    public class offVertex
    {
        public Vector3 vertex { get; set; } //the vertex information

        public float[] setColor = new float[4]; //if colorinformation needed

        public bool isColored { get; set; } //color stuff

        public Vector3 normalVector {get; set;}
        public float x { get; set; }
        public float y { get; set; }
        public float z { get; set; }

        //constructors
        public offVertex( )
        {
            this.vertex = new Vector3(0.0f, 0.0f, 0.0f);
            this.setColor = null;
            this.isColored = false;
            this.normalVector = new Vector3(1.0f, 1.0f, 1.0f);
            this.x = this.y = this.z = 0.0f;
        }
        public offVertex(float X, float Y, float Z)
        {
            this.vertex = new Vector3(X, Y, Z);
            this.setColor = null;
            this.isColored = false;
            this.normalVector = new Vector3(0.0f, 0.0f, 1.0f);
            this.x = X;
            this.y = Y;
            this.z = Z;
        }
        public offVertex(float X, float Y, float Z, float[] Color)
        {
            this.vertex = new Vector3(X, Y, Z);
            this.setColor = Color;
            this.isColored = true;
            this.normalVector = new Vector3(0.0f, 0.0f, 1.0f);
            this.x = X;
            this.y = Y;
            this.z = Z;
        }


        //methods
    }


    //this stores the polygon information from an OFF file
    public class offPolygon
    {
        //the list of verticies taken for this polygon
        public List<offVertex> verticies = new List<offVertex>();

        public int vCount { get; set; } //#of verticies
        public float[] Color = new float[4]; //for future releases
        public bool isColored { get; set; } //for future releases
        public Vector3 normalVector = new Vector3();

        //default constructor
        public offPolygon()
        {
            //dont add anything 
            this.vCount = verticies.Count();
            this.normalVector = new Vector3(0.0f, 0.0f, 1.0f);
            this.Color = null;
            this.isColored = false;
        }
        //if its defined to have a color
        public offPolygon(float[] color)
        {
            //dont add anything 
            this.vCount = verticies.Count();
            this.normalVector = new Vector3(0.0f, 0.0f, 1.0f);
            this.Color = color;
            this.isColored = true;
        }

        public void insertVertex(offVertex newVertex) //given a vertex, add it to this polly gon
        {
            this.verticies.Add(newVertex);
            this.vCount = verticies.Count();
        }

        public void removeVertex(int index)
        {
            this.verticies.RemoveAt(index);
            this.vCount = this.verticies.Count();
        }

        public void removeVertex(offVertex item)
        {
            this.verticies.Remove(item);
            this.vCount = this.verticies.Count();
        }

        //im pretty sure we need to calculate the normal vector of this thing
        public void findNormal()
        {
            Vector3 a, b, c, edge1, edge2;
            if (vCount < 3) //you need at least 3 points to make a polygon
                return;
             //you only need 3 points to make a plane
            //and find your vector

            //yay lin algebra!
            a = this.verticies[0].vertex;
            b = this.verticies[1].vertex;
            c = this.verticies[2].vertex;

            edge1 = new Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
            edge2 = new Vector3(b.X - c.X, b.Y - c.Y, b.Z - c.Z);

            this.normalVector.X = ((edge1.Y * edge2.Z) - (edge1.Z * edge2.Y));
            this.normalVector.Y = ((edge1.Z * edge2.X) - (edge1.X * edge2.Z));
            this.normalVector.Z = ((edge1.X * edge2.Y) - (edge1.Y * edge2.X));
        }

    }
}
